package com.open.capacity.notice.processor;

import java.io.IOException;
import java.util.Date;

import org.apache.commons.collections.MapUtils;
import org.springframework.transaction.support.TransactionTemplate;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.open.capacity.notice.dmo.SendMessageLog;
import com.open.capacity.notice.dmo.SendMessageTask;
import com.open.capacity.notice.enums.SendMessageTaskStatus;
import com.open.capacity.notice.provider.MessageError;
import com.open.capacity.notice.provider.NoticeTemplateFacotoy;
import com.open.capacity.notice.provider.core.http.interactive.req.ThRequest;
import com.open.capacity.notice.service.SendMessageJobService;
import com.open.capacity.notice.service.SendMessageLogService;
import com.open.capacity.notice.service.SendMessageTaskService;

import lombok.Getter;

/**
 * 抽象发送任务 
 *
 */
@Getter
public abstract class AbstractCreateTaskProcessor implements CreateTaskProcessor {

	private SendMessageTaskService sendMessageTaskService;
	private SendMessageJobService sendMessageJobService;
	private SendMessageLogService sendMessageLogService;
	private TransactionTemplate transactionTemplate;

	private ObjectMapper objectMapper;

	protected NoticeTemplateFacotoy noticeTemplateFacotoy;

	public AbstractCreateTaskProcessor(ObjectMapper objectMapper, SendMessageTaskService sendMessageTaskService,
			SendMessageJobService sendMessageJobService, SendMessageLogService sendMessageLogService,
			TransactionTemplate transactionTemplate, NoticeTemplateFacotoy noticeTemplateFacotoy) {
		super();
		this.objectMapper = objectMapper;
		this.sendMessageTaskService = sendMessageTaskService;
		this.sendMessageJobService = sendMessageJobService;
		this.sendMessageLogService = sendMessageLogService;
		this.transactionTemplate = transactionTemplate;
		this.noticeTemplateFacotoy = noticeTemplateFacotoy;
	}

	/**
	 * 选择发送渠道模板类型发送
	 * 
	 * @param <REQ>
	 * @param request
	 * @param task
	 * @throws IOException
	 */
	public <REQ extends ThRequest> void sendMessage(REQ request, SendMessageTask task) throws IOException {
		SendMessageLog sendMessageLog = SendMessageLog.builder().sendType(task.getSendType()).taskId(task.getId())
				.requestBody(objectMapper.writeValueAsString(request)).build();
		sendMessageLogService.save(sendMessageLog);
		task.setSendTime(new Date());
		noticeTemplateFacotoy.getNoticeTemplate(task.getSendType()).sendText(request, (res) -> {
			if (MapUtils.getBooleanValue(res, "flag")) {
				task.setStatus(SendMessageTaskStatus.SUCCESS);
				sendMessageLog.setResponseCode(MapUtils.getString(res, "code"));
				sendMessageLog.setResponseBody(MapUtils.getString(res, "message"));
				saveTable(task, sendMessageLog);
			} else {
				task.setFailure(MapUtils.getString(res, "message"));
				task.setStatus(SendMessageTaskStatus.FAILURE);
				sendMessageLog.setResponseCode(MapUtils.getString(res, "code"));
				sendMessageLog.setResponseBody(MapUtils.getString(res, "message"));
				saveTable(task, sendMessageLog);
			}
		}, (exception) -> {
			task.setFailure(MessageError.CONNECT_TIMEOUT_ERROR.getErrorMessage());
			task.setStatus(SendMessageTaskStatus.FAILURE);
			sendMessageLog.setResponseCode(MessageError.CONNECT_TIMEOUT_ERROR.getErrorCode());
			sendMessageLog.setResponseBody(MessageError.CONNECT_TIMEOUT_ERROR.getErrorMessage());
			saveTable(task, sendMessageLog);

		});
	}

	private void saveTable(SendMessageTask task, SendMessageLog sendMessageLog) {
		sendMessageTaskService.updateById(task);
		sendMessageLogService.updateById(sendMessageLog);

	}

}
